package replaceStringUtil;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
 * 手动指定pageUtil版本，要求By和SmsSeleniumUtil.assertTrue(SmsSeleniumUtil.isDisplayed(SmsSeleniumUtil.getCachedElement中的数量相等
 * @author XIAOBAI123
 *
 */
public class StringReplaceString04 {

	List<String> des = new ArrayList<String>();
	List<String> sou = new ArrayList<String>();
	List<String> my = new ArrayList<String>();

	public static void main(String[] args) {

		StringReplaceString04 srs = new StringReplaceString04();
		
		System.out.println("请输入你想要替换的pageUtil名称: ");
		Scanner sc = new Scanner(System.in);
		String pageUtil = sc.nextLine();
		
		
		srs.readfile();
		
		srs.replaceString("SmsSeleniumUtil.assertTrue(SmsSeleniumUtil.isDisplayed(SmsSeleniumUtil.getCachedElement(" + pageUtil + ".",
				")));", "public final static By", " = By.");
		srs.getAssertEquals();
		
		System.out.println("柏大爷您好！我已光荣完成任务，欧耶!");
		System.out.println();
		System.out.println("©2012 xiaobai 京ICP证888888号 版权所有,盗版必究!");
		System.out.println();
		System.out.println("记得要更改equals哦！！！最后我祝柏大爷天天开心，身体健康，出门捡钱 O_O");
	}
	
	/** 读取要替换的文件和要截取的文件的内容存储到List集合中*/
	private void readfile() {
		File file1 = new File("F:/截取.txt");
		File file2 = new File("F:/替换.txt");
		
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(file1));
			while (br.ready()) {
				String s = br.readLine();
				des.add(s.trim());
			}

			br = new BufferedReader(new FileReader(file2));
			while (br.ready()) {
				String s = br.readLine();
				sou.add(s.trim());
			}

		} catch (IOException e) {
			System.out.println("replace err...");
			e.printStackTrace();
		} finally {
			try {
				if(br != null)
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
	
	/** 从集合中读取要替换的内容和要截取用来替换的内容并进行替换，将结果存入List集合中*/
	private void replaceString(String startDes, String endDes, String startSou,	String endSou) {
		if (des.size() != 0 && sou.size() != 0) {
			for (int i = 0; i < des.size(); i++) {
				if (des.get(i).startsWith(startSou)	&& des.get(i).contains(endSou)) {
					String[] start = des.get(i).split(startSou);
					String[] end = start[1].split(endSou);
					if (sou.get(i).startsWith(startDes)	&& sou.get(i).endsWith(endDes)) {
						my.add(startDes + end[0].trim() + endDes);
					}
				}
			}
		}
	}

	/** 从集合中读出assertTrue的内容并将内容进行新的替换，将替换结果保存在另外一个assertEquals集合中。将assertTrue的内容写到目标文件中*/
	private void getAssertEquals() {
		
		List<String> lastStringList = new ArrayList<String>();
		BufferedWriter bw = null;
		try {
			File file = new File("F:/替换后的文本.txt");
			if (file.exists()) {
				file.delete();
				file = new File("F:/替换后的文本.txt");
			}
			
			for(int i=0; i<my.size(); i++){
				String s = my.get(i).replaceAll("assertTrue", "assertEquals");
				if(s.trim().endsWith(")));")){
					lastStringList.add(s.replaceAll("SmsSeleniumUtil.isDisplayed\\(", "\"\", ").replaceAll("\\)\\)\\)\\;", ").getText().trim());"));
				}else{
					lastStringList.add(s);
				}
			}
			
			bw = new BufferedWriter(new FileWriter(file));
			bw.write("\r");
			bw.write("\n");
			
			bw.write("assertTrue start:");
			bw.write("\r");
			bw.write("\n");
			bw.write("\r");
			bw.write("\n");
			for (int i = 0; i < my.size(); i++) {
				bw.write(my.get(i));
				bw.write("\r");
				bw.write("\n");
			}
			
			bw.write("\r");
			bw.write("\n");
			bw.write("\r");
			bw.write("\n");
			bw.write("assertEquals start:");
			bw.write("\r");
			bw.write("\n");
			bw.write("\r");
			bw.write("\n");
			
			for (int i = 0; i < lastStringList.size(); i++) {
				bw.write(lastStringList.get(i));
				bw.write("\r");
				bw.write("\n");
			}
			
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}finally{
			try {
				if(bw != null)
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}